// pages/vippromotion/vip promotion.js
import { HTTP } from '../../utils/http.js'
import { config } from '../../utils/config'
const app = getApp();
let http = new HTTP()
Page({
  data: {
    newUrl:'https://images.weigeee.com',
    imgUrl: config.imgUrl,
    idFotter:'99元成为会员',
    buttonClicked:false,
    _num: 0,
    maskShow:false
  },
  onLoad:function(options){
    let is_member = wx.getStorageSync('is_member');
    if(is_member == 1){
      this.setData({
        idFotter: '已是味歌会员',
        _num: 1
      })
      
    }else{
      this.setData({
        idFotter: '99元成为会员'
      })
    }
    if(options.code){
          http.request({
            url: '/client/bind',
            method:'post',
            header: {
              token: wx.getStorageSync('token')
            },
            data: {
              invite_code: decodeURIComponent(options.code)
            },
            success:function(res){
              console.log(res)
            }
          })
      }
  },
  onShow: function () {
    this.reqCode()
  },
  toDaiLi(){
    wx.navigateTo({
      url: '/pages/agency/agency',
    })
  },
  closeMask() {  //关闭支付完成界面
    this.setData({
        maskShow: false
      })
  },
  toPay(){
    this.setData({
      buttonClicked: true
    })
    var that = this
    console.log(wx.getStorageSync('token'))
      http.request({
        url:'/client/createMemberOrder',
        header: {
          token: wx.getStorageSync('token')
        },
       
        success: (res) => {
          try{
            wx.setStorageSync('order_no', res.data.order_no)
          }catch(e){}
          http.request({
            url:'/client/openMemberPay',
            header: {
              token: wx.getStorageSync('token')
            },
            data: {
              order_no: res.data.order_no,
              pay_method:'wx'
            },
            success: (res) =>{
                console.log('已经调用支付', res)
                wx.showToast({title: '加载中', icon: 'loading', duration: 1500});
                that.requestPayment(res.data)
            },
            fail: () => {}
          })
        },
        fail: function (err) { // 错误反馈
          console.log('http-err', err)
        }
      })  
  },
  //申请支付
  requestPayment: function(obj){
    var that =this
      wx.hideToast()
      wx.requestPayment({
        'timeStamp': obj.timeStamp,
        'nonceStr': obj.nonceStr,
        'package': obj.package,
        'signType': 'MD5',
        'paySign': obj.paySign,
        'success':function(res){
          http.request({
            url:'/client/openMemberPayNotify',
            header: {
              token: wx.getStorageSync('token')
            },
            data: {
              order_no: wx.getStorageSync('order_no')
            },
            success: (res) =>{
              if(res.code == 200){
                // wx.navigateTo({
                //   url: '/pages/my/my'
                //   })
                 that.setData({
                    maskShow:true,
                    idFotter: '已是味歌会员',
                    _num: 1
                  })
                  wx.setStorageSync('is_member', '1')
              }
            }
        })
        },
        'fail':function(res){
            wx.showToast({
              title: "支付取消",
              icon: 'none',
              duration: 1500
            })
            that.setData({
              buttonClicked: false
            })
        }
      })
    },
  //获取用户邀请码
  reqCode(){
    http.request({
        url:'/client/getInviteCode',
        header:{
            'token' : wx.getStorageSync("token")
        },
        success: res =>{
            console.log(res.data.invite_code)
            try{
                wx.setStorageSync('invcode', res.data.invite_code)
            }catch(e){}
        }
      })
    },
  //右上角分享功能
  onShareAppMessage: function (res) {
    var url = "/pages/vippromotion/vippromotion?code="
    let iscode =wx.getStorageSync('invcode')
    return {
      title: '我每天都按味歌推荐的菜谱下单买菜，帮我打开助力得…',
      path: '/pages/index/index?GoTo=' + encodeURIComponent( url + iscode),
      // imageUrl: '../../images/vip_share.jpg',
      imageUrl: '../../images/20200808175233.jpg',
      success: function (res) {
        // 转发成功
        wx.showToast({
          title: "分享成功",
          icon: 'success',
          duration: 1500
        })
      },
      fail: function (res) {
        // 转发失败
        wx.showToast({
          title: "分享失败",
          duration: 2001
        })
      }
    }
  },
  
})
